﻿using AuthTemplate.DTO.User;
using AuthTemplate.Infrastructure;
using AuthTemplate.Infrastructure.Token;
using AuthTemplate.MVC.Filter;
using AuthTemplate.Service;
using AuthTemplate.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace AuthTemplate.MVC.Controllers
{
    
    [LoginFilter]
    [RoutePrefix("user")]
    public class UserManagerController : BaseController
    {
        private readonly IUserService userService;

        public UserManagerController(IUserService userService)
        {
            this.userService = userService;
        }

        [HttpPost]
        [Route("add")]
        public string Add(AddUserInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                userService.AddUser(input);
            }
            catch(Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败" + ex.Message;
            }
            return JsonHelper.Serialize(resp);
        }

        [HttpPost]
        [Route("changepwd")]
        public string ChangePwd(ChangeUserPwdInput input)
        {
            MyResponse resp = new MyResponse();
            try
            {
                string token = HttpContext.Request.Headers["Authorization"].Substring(7).Trim();
                string UserAccount = CommonToken.DecodeToken(token, false).UserName;

                input.Account = UserAccount;
                userService.ChangeUserPwd(input); 
            }
            catch(Exception ex)
            {
                resp.Code = 500;
                resp.Message = "操作失败"+ex.Message;
            }

            return JsonHelper.Serialize(resp);
        }

        
    }
}